package com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.info;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.PatrolInfo;
import com.hengxinguotong.hxgtwygsp.model.Year;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseFragment;
import com.hengxinguotong.hxgtwygsp.widget.DropListPW;
import com.hengxinguotong.hxgtwygsp.widget.RecycleViewDivider;
import com.hengxinguotong.hxgtwygsp.widget.YearPW;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class PatrolInfoFragment extends BaseFragment implements PatrolInfoContract.IView
        , DropListPW.OnClickCallback<Year> {
    @Inject
    PatrolInfoContract.IPresenter mPresenter;

    @BindView(R.id.patrol_info_recycler)
    RecyclerView patrolInfoRecycler;
    @BindView(R.id.patrol_year)
    TextView mPatrolYear;
    @BindView(R.id.patrol_year_container)
    RelativeLayout mPatrolYearContainer;

    private List<Year> yearList;

    private List<PatrolInfo> patrolInfoList = new ArrayList<>();;
    private PatrolInfoAdapter patrolInfoAdapter;

    public PatrolInfoFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter.bindView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_patrol_info;
    }

    @Override
    public void init() {
        yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        mPatrolYear.setText(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
        Year year;
        for (int i = 0; i < 3; i++)
        {
            year = new Year();
            year.setName(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
            yearList.add(year);
            calendar.add(Calendar.YEAR, -1);
        }

        mockView();
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        patrolInfoRecycler.setLayoutManager(llm);
        patrolInfoRecycler.addItemDecoration(new RecycleViewDivider(getContext()));
        patrolInfoAdapter = new PatrolInfoAdapter(R.layout.item_patrol_info,patrolInfoList);
        patrolInfoRecycler.setAdapter(patrolInfoAdapter);
    }

    private void mockView() {
        patrolInfoList.add(new PatrolInfo("2019/06/28","王俊",24,18.2,14.6,11.5,4,84.6,"已整改"));
        patrolInfoList.add(new PatrolInfo("2019/03/25","王自威",9.5,11.5,11.3,8,2.6,42.9,"已整改"));
    }

    @OnClick(R.id.patrol_year_container)
    public void onViewClicked(View view) {
        YearPW yearPW = new YearPW(getContext(), view.getWidth());
        yearPW.setOnClickCallback(this);
        yearPW.initData(yearList, 0);
        yearPW.showAsDropDown(view);
    }


    @Override
    public void onClickPW(Year year) {
        mPatrolYear.setText(year.getName());
    }


}
